/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ClasificacionDePizzasAndOperationBox.java
 *
 * Created on 10-ene-2010, 2:19:22
 */
package clasificaciondepizzas;

import clasificaciondepizzas.models.SauceAndToppingModel;
import clasificaciondepizzas.operations.ImageOperations;
import clasificaciondepizzas.operations.ValueSauceAndToppingOperations;
import com.sun.media.jai.widget.DisplayJAI;
import javax.media.jai.PlanarImage;
import javax.swing.JOptionPane;

/**
 *
 * @author Manuel
 */
public class ClasificacionDePizzasAndOperationBox extends javax.swing.JDialog {

    /** Creates new form ClasificacionDePizzasAndOperationBox */
    public ClasificacionDePizzasAndOperationBox(java.awt.Frame parent) {
        super(parent);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        andOperationImageScrollPane = new javax.swing.JScrollPane();
        acceptButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(clasificaciondepizzas.ClasificacionDePizzasApp.class).getContext().getResourceMap(ClasificacionDePizzasAndOperationBox.class);
        setTitle(resourceMap.getString("andOperationBox.title")); // NOI18N
        setModal(true);
        setName("andOperationBox"); // NOI18N
        setResizable(false);

        andOperationImageScrollPane.setViewportBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("andOperationImageScrollPane.viewportBorder.title"))); // NOI18N
        andOperationImageScrollPane.setName("andOperationImageScrollPane"); // NOI18N

        acceptButton.setText(resourceMap.getString("acceptButton.text")); // NOI18N
        acceptButton.setName("acceptButton"); // NOI18N
        acceptButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                acceptButtonActionPerformed(evt);
            }
        });

        cancelButton.setText(resourceMap.getString("cancelButton.text")); // NOI18N
        cancelButton.setName("cancelButton"); // NOI18N
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(andOperationImageScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(acceptButton, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE)
                    .addComponent(cancelButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(andOperationImageScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(acceptButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelButton)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void acceptButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_acceptButtonActionPerformed
        PlanarImage image = ImageOperations.and(andOperationImage, originalImage);
        ValueSauceAndToppingOperations vsto = new ValueSauceAndToppingOperations(image);

        stm.setMeanOfHue(vsto.meanOfHue());
        stm.setStandardDeviationOfHue(vsto.standardDeviationOfHue());
        stm.setMeanOfSaturation(vsto.meanOfSaturation());
        stm.setStandardDeviationOfSaturation(vsto.standardDeviationOfSaturation());
        stm.setModified();

        setVisible(false);
        dispose();
    }//GEN-LAST:event_acceptButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton acceptButton;
    private javax.swing.JScrollPane andOperationImageScrollPane;
    private javax.swing.JButton cancelButton;
    // End of variables declaration//GEN-END:variables
    private PlanarImage originalImage;
    private PlanarImage andOperationImage;
    private PlanarImage andOperationThumbnail;
    private SauceAndToppingModel stm;

    void setImage(PlanarImage image) {
        originalImage = stm.getOriginalImage();
        andOperationImage = ImageOperations.and(image, originalImage);
        andOperationThumbnail = andOperationImage;

        PlanarImage previewAndOperationImage; //previsualización en miniatura de la operación AND

        // si la imagen supera el tamaño del panel con desplazamiento se escala a un tamaño menor
        if (image.getWidth() > andOperationImageScrollPane.getWidth() || image.getHeight() > andOperationImageScrollPane.getHeight()) {
            // si la imagen supera la anchura del panel con desplazamiento se escala a una anchura menor
            if (image.getWidth() > image.getHeight()) {
                float ratio = (float) andOperationImageScrollPane.getWidth() / (float) image.getWidth();
                andOperationThumbnail = ImageOperations.scale(andOperationImage, ratio, ratio);
            } // si la imagen supera la altura del panel con desplazamiento se escala a una altura menor
            else {
                float ratio = (float) andOperationImageScrollPane.getHeight() / (float) image.getHeight();
                andOperationThumbnail = ImageOperations.scale(andOperationImage, ratio, ratio);
            }
        }

        try {
            // previsualización en miniatura del suavizado
            previewAndOperationImage = andOperationThumbnail;
            DisplayJAI display = new DisplayJAI(previewAndOperationImage);
            andOperationImageScrollPane.setViewportView(display);
        } catch (IllegalArgumentException e) {
            JOptionPane.showMessageDialog(this,
                    "No es posible aplicar el operador AND.",
                    "Error",
                    JOptionPane.ERROR_MESSAGE);
        } catch (RuntimeException e) {
            JOptionPane.showMessageDialog(this,
                    "No es posible previsualizar la imagen.",
                    "Error",
                    JOptionPane.ERROR_MESSAGE);
        }
    }

    void setSauceAndToppingModel(SauceAndToppingModel stm) {
        this.stm = stm;
    }
}
